package com.academy.student;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.academy.querystapp.R;
import com.academy.services.BaseService;
import com.academy.services.RequestMethod;
import com.academy.services.ResponseListener;

public class StudentForgetPassword extends Activity implements OnClickListener,ResponseListener
{
	EditText edtUserID;
	Button btnGetPassword,btnClear;
	String UserID;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forgetpassword);
		initializeViews();
	}

	
	public void initializeViews()
	{
		edtUserID=(EditText) findViewById(R.id.edtMobileNo);
		btnClear=(Button) findViewById(R.id.btnClear);
		btnGetPassword=(Button) findViewById(R.id.btnForgetpassword);
		
		btnClear.setOnClickListener(this);
		btnGetPassword.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
	
		if(v==btnClear)
		{
			edtUserID.setText("");
		}
	
		
		if(v==btnGetPassword)
		{
			UserID=edtUserID.getText().toString().trim();
			
			
			if(UserID.equals(""))
			{
				
				Builder builder = new Builder(this);
				builder.setTitle("Forget Password");
				builder.setMessage("Please Enter the UserID!");
				builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int arg1)
					{	
						dialog.dismiss();
					}
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			}
			else if(UserID.length()<8||UserID.length()>8)
			{
				
				Builder builder = new Builder(this);
				builder.setTitle("Forget Password");
				builder.setMessage("Please Enter your 8 digit Student ID !");
				builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int arg1)
					{	
						dialog.dismiss();
					}
				});
				AlertDialog alertDialog = builder.create();
				alertDialog.show();
			}
			else
			{				
				
		    					
				BaseService baseService = new BaseService(this, this, RequestMethod.GET);
				baseService.addParam(getString(R.string.userName_param), edtUserID.getText().toString());
				baseService.addParam(getString(R.string.userType_Param),"Student");
				baseService.execute(getString(R.string.loginStudentURL));
		    					
		    				      
		    				      	
		    				     
			}
		}
		
	}


	private void parseResponse(String response)
	{
		if(response==null ||response.equals("null"))
		{
			System.out.println("Response ---> "+response);
						
			showAlert(null, "No Server Found!");
		}
		else
		{
			try 
			{ 
			
				JSONObject RootObject = new JSONObject(response);
				System.out.println("JSON Object==> "+RootObject.toString());
				JSONObject studentObject = RootObject.getJSONObject("student");
				JSONObject studentData=studentObject.getJSONObject("StudentData");
				String strStatus=studentData.getString("status");
				System.out.println("studentData="+studentData);

				if(strStatus.equals("true"))
				{					
					System.out.println("stufent pass"+studentData.getString("password"));
					if(studentData.getString("password")!=null)
					{
						showAlert("Forget Password", "Your Password is "+studentData.getString("password"));	
					}
				}
				else if(strStatus.equals("false"))
				{
					String strError= studentData.getString("errorDesc");
					showAlert(null, strError);
				}
			}
			catch (JSONException e) 
			{
				e.printStackTrace();
			} 
		}
	}
	
	
	public void showAlert(String Title,String Message)
	{
		Builder builder = new Builder(this);
		builder.setTitle(Title);
		builder.setMessage(Message);
		builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int arg1)
			{	
//				Intent i=new Intent(StudentActivity.this,Homepage.class);
//				startActivity(i);
//				finish();						
			}
		});
		AlertDialog alertDialog = builder.create();
		alertDialog.show();	
	}

	@Override
	public void onServiceCompleted(String response, String requestURL) {
		parseResponse(response);
		
	}

	@Override
	public void onServiceFailed(String exception, String requestURL) {
		// TODO Auto-generated method stub
		
	}

}
